package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.util.Comutils;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class PointExchangeBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(PointExchangeBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String paymentSerialNbr = bizParam.getParamString(BizConsts.KEY_ESPECIAL_PAYMENT_SERIAL_NBR);
            String bankCodeSet = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE_SET);
            String accNbr = bizParam.getParamString(BizConsts.KEY_ESPECIAL_ACC_NBR);
            String destinationattr = bizParam.getParamString(BizConsts.KEY_ESPECIAL_DESTINATIONATTR);
            String productId = bizParam.getParamString(BizConsts.KEY_ESPECIAL_PRODUCTID);
            String bankCode = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE);
            String cityId = bizParam.getParamString(BizConsts.KEY_ESPECIAL_CITYID);
            String balanceItemTypeID = "";
            if (cityId.equals("1001")) {
                balanceItemTypeID = "12313";
            } else {
                balanceItemTypeID = "302744";
            }

            String chargeSource = "";
            String[] bankCodeSets = bankCodeSet.split(",");
            for (String item : bankCodeSets) {
                if (String.valueOf(item.split("\\|")[0]).equals(bankCode)) {
                    chargeSource = String.valueOf(item.split("\\|")[1]);
                }
            }


            JSONObject recharge = new JSONObject();
            recharge.put("reqSerial", paymentSerialNbr);
            recharge.put("chargeSource", chargeSource);
            recharge.put("destinationId", accNbr);
            recharge.put("destinationIdType", "2");
            recharge.put("destinationAttr", destinationattr);
            recharge.put("balanceItemTypeID", balanceItemTypeID);
            recharge.put("unitTypeID", "0");
            recharge.put("feeAmount", productId);
            recharge.put("BankCode", bankCode);
            recharge.put("PaymentMethod", "11");

            JSONObject stdCcrRecharge = new JSONObject();
            stdCcrRecharge.put("recharge", recharge);
            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            jsonObject.put("stdCcrRecharge", stdCcrRecharge);

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());

            //新接口新域名
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

            String resp = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);
            StringBuffer buffer = new StringBuffer();
            if (resp.equals("")) {
                resp = com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
                buffer = buffer.append(resp);
            } else {
                //解析json 拼接报文
                JSONObject json = JSON.parseObject(resp);
                jsonToXmlStr(json, buffer, accNbr, paymentSerialNbr);
            }

            bizResult.setHttpBody(buffer.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(JSONObject json, StringBuffer buffer, String accNbr, String paymentSerialNbr) {
        if (String.valueOf(json.get("errorCode")).equals("-1")) {
            buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            buffer.append("<response>");
            buffer.append("<Result>" + (json.get("errorCode") == null ? "" : json.get("errorCode")) + "</Result>");
            buffer.append("<Result_desc>" + (json.get("errorMsg") == null ? "" : json.get("errorMsg")) + "</Result_desc>");
            buffer.append("<Acc_Nbr>" + accNbr + "</Acc_Nbr>");
            buffer.append("<Payment_Serial_Nbr>" + paymentSerialNbr + "</Payment_Serial_Nbr>");
            //<UsedPoint>1</UsedPoint>   --还是取point.Amount映射的   ????
            buffer.append("<UsedPoint>" + "" + "</UsedPoint>");
            buffer.append("<CurrMonthPoint>" + "" + "</CurrMonthPoint>");
            buffer.append("</response>");
        } else if (String.valueOf(json.get("errorCode")).equals("0")) {
            buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            buffer.append("<response>");
            buffer.append("<Result>" + (json.get("errorCode") == null ? "" : json.get("errorCode")) + "</Result>");
            buffer.append("<Result_desc></Result_desc>");
            buffer.append("<Acc_Nbr>" + accNbr + "</Acc_Nbr>");
            buffer.append("<Payment_Serial_Nbr>" + paymentSerialNbr + "</Payment_Serial_Nbr>");
            //<UsedPoint>1</UsedPoint>   --还是取point.Amount映射的   ????
            buffer.append("<UsedPoint>" + "" + "</UsedPoint>");
            //<CurrMonthPoint>715</CurrMonthPoint>   --取应答消息的surplusPoint ????
            buffer.append("<CurrMonthPoint>" + "" + "</CurrMonthPoint>");
            buffer.append("</response>");
        }
    }
}
